<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.prime.com.tr/ui">

<ui:composition template="/template/template-padrao.xhtml">

	<ui:define name="titulo">Vender veículos</ui:define>

	<ui:define name="baseCorpo">
		
		<h:form id="venderVeiculosForm">
		
			<script type="text/javascript">
				jQuery(function(){
					
					jQuery('input[id*=valor]').maskMoney({symbol:'R$ ', showSymbol:true, thousands:'.', decimal:',', symbolStay: true});
					
				});
				
			</script>
		
			<h1 class="titulo-tela">Vender veículos</h1>

			<p:panel id="panelVenderVeiculo">
			
			<br />
			<h1 class="ui-panel-titlebar ui-widget-header ui-corner-all">Dados do veículo</h1>
			
				<table cellspacing="10" width="100%">
					<tr>
						<td>
							<h:outputLabel for="placa" value="* Placa:" />
							<br />
							<p:autoComplete id="placa" minQueryLength="2" label="Placa"
							 	value="#{venderVeiculosBean.vendaVeiculo.veiculo}"
	                    		completeMethod="#{venderVeiculosBean.consultarVeiculosVendaPorPlaca}" 
	                    		var="veic" itemLabel="#{veic.nrPlaca}" itemValue="#{veic}"	
	                    		converter="veiculoConverter"
	                    		selectListener="#{venderVeiculosBean.handleSelectVeiculo}"
	                    		onSelectUpdate="fabricante, modelo, anoFabricacao, anoModelo, cor, valorCompra, valorVenda" /> 
						</td>
						<td>
							<h:outputLabel for="fabricante" value="Fabricante:" />
							<br />
							<p:inputText id="fabricante" disabled="true" readonly="true" styleClass="ui-state-disabled"
								value="#{venderVeiculosBean.vendaVeiculo.veiculo.fabricante.nmFabricante}"
								label="Fabricante" style="width:220px;" />
						</td>
						<td>
							<h:outputLabel for="modelo" value="Modelo:" />
							<br />
							<p:inputText id="modelo" disabled="true" readonly="true" styleClass="ui-state-disabled"
								value="#{venderVeiculosBean.vendaVeiculo.veiculo.modelo.nmModelo}"
								label="Modelo" style="width:200px;" />
						</td>
					</tr>
					
					<tr>
						<td>
							<h:outputLabel for="anoFabricacao" value="Ano de fabricação:" />
							<br />
							<p:inputText id="anoFabricacao" disabled="true" readonly="true" styleClass="ui-state-disabled"
								value="#{venderVeiculosBean.vendaVeiculo.veiculo.anoFabricacao}" label="Ano"
								style="width:45px;" />
						</td>
						<td>
							<h:outputLabel for="anoModelo" value="Ano do modelo:" />
							<br />
							<p:inputText id="anoModelo" disabled="true" readonly="true" styleClass="ui-state-disabled"
								value="#{venderVeiculosBean.vendaVeiculo.veiculo.anoModelo}" label="Ano"
								style="width:45px;" />
						</td>
						<td>
							<h:outputLabel for="cor" value="Cor:" />
							<br />
							<p:inputText id="cor" disabled="true" readonly="true" styleClass="ui-state-disabled"
								value="#{venderVeiculosBean.vendaVeiculo.veiculo.cor}" label="Cor" />
						</td>
					</tr>
					
					<tr>
						<td>
							<h:outputLabel for="valorCompra" value="Valor de compra:" />
							<br />
							<p:inputText id="valorCompra" disabled="true" readonly="true" styleClass="ui-state-disabled"
								converter="moedaDoubleConverter"
								value="#{venderVeiculosBean.vendaVeiculo.veiculo.valorCompra}" />
						</td>
						<td>
							<h:outputLabel for="valorVenda" value="* Valor de venda:" />
							<br />
							<p:inputText id="valorVenda" converter="moedaDoubleConverter"
								value="#{venderVeiculosBean.vendaVeiculo.veiculo.valorVenda}" />
						</td>
					</tr>
				</table>
				
			<br />			
			<h1 class="ui-panel-titlebar ui-widget-header ui-corner-all">Dados do cliente</h1>
			
				<table cellspacing="10" width="100%">
					<tr>
						<td>
							<h:outputLabel for="cliente" value="* Cliente:" />
							<br />
							<p:autoComplete id="cliente" minQueryLength="2" label="Cliente" maxlength="100"
							 	value="#{venderVeiculosBean.vendaVeiculo.cliente}"
	                    		completeMethod="#{venderVeiculosBean.consultarPessoasPorNome}" 
	                    		var="pess" itemLabel="#{pess.nmPessoa}" itemValue="#{pess}"	
	                    		converter="pessoaConverter"
	                    		selectListener="#{venderVeiculosBean.handleSelectPessoa}"
	                    		onSelectUpdate="cpfPessoa, fonePessoa, celularPessoa"
	                    		style="width:250px;" /> 
							
							<p:commandButton update="clienteFornecedorModalForm"
								onclick="dlgClienteFornecedor.show();" image="ui-icon ui-icon-plusthick" 
								style="margin-left:5px;" >
								<f:setPropertyActionListener value="#{false}"
			                    	target="#{manterClientesFornecedoresBean.cadastroDespachante}" />
		                    	<f:setPropertyActionListener value="#{true}"
			                    	target="#{manterClientesFornecedoresBean.telaVenderVeiculos}" />
							</p:commandButton>
						</td>
						<td>
							<h:outputLabel for="cpfPessoa" value="CPF:" />
							<br />
							<p:inputText id="cpfPessoa" disabled="true" readonly="true" styleClass="ui-state-disabled"
								value="#{venderVeiculosBean.vendaVeiculo.cliente.cpfCnpj}"
								label="CPF" />
						</td>
						<td>
							<h:outputLabel for="fonePessoa" value="Fone:" />
							<br />
							<p:inputText id="fonePessoa" disabled="true" readonly="true" styleClass="ui-state-disabled"
								value="#{venderVeiculosBean.vendaVeiculo.cliente.meioComunicacao.nrTelefone}"
								label="Fone" />
						</td>
						<td>
							<h:outputLabel for="celularPessoa" value="Celular:" />
							<br />
							<p:inputText id="celularPessoa" disabled="true" readonly="true" styleClass="ui-state-disabled"
								value="#{venderVeiculosBean.vendaVeiculo.cliente.meioComunicacao.nrCelular}"
								label="Celular" />
						</td>
					</tr>				
				</table>
				
			<br />			
			<h1 class="ui-panel-titlebar ui-widget-header ui-corner-all">Dados do pagamento</h1>
				
				<table cellspacing="10">
				
					<tr>
						<td>
							<h:outputLabel for="valor" value="Valor:" />
							<br />
							<p:inputText id="valor" maxlength="100" style="width:150px; "
								value="#{venderVeiculosBean.valor}" converter="moedaDoubleConverter" />
			            </td>
			            <td>
							<h:outputLabel for="data" value="Data do vencimento:" />
							<br />
							<p:calendar id="data" label="Data" pattern="dd/MM/yyyy"
								maxlength="10" value="#{venderVeiculosBean.data}" />
			            </td>
		            </tr>
		            <tr>
			            <td>
							<h:outputLabel for="categoria" value="Categoria:" />
							<br />
							<h:selectOneMenu id="categoria" label="Categoria" style="width:250px;"
								styleClass="ui-inputfield ui-widget ui-state-default ui-corner-all"
								value="#{venderVeiculosBean.idCategoria}" >
							    <f:selectItems value="#{venderVeiculosBean.todasCategorias}" var="cat"
							    	itemValue="#{cat.value}" itemLabel="#{cat.label}"/>
							</h:selectOneMenu>
			            </td>
			            <td>
							<h:outputLabel for="conta" value="Conta:" />
							<br />
							<h:selectOneMenu id="conta" label="Conta" style="width:250px;"
								styleClass="ui-inputfield ui-widget ui-state-default ui-corner-all"
								value="#{venderVeiculosBean.idConta}" >
							    <f:selectItems value="#{venderVeiculosBean.todasContas}" var="cont"
							    	itemValue="#{cont.value}" itemLabel="#{cont.label}"/>
							</h:selectOneMenu>
			            </td>
			            <td>
							<h:outputLabel for="formaPagamento" value="Forma de pagamento:" />
							<br />
							<h:selectOneMenu id="formaPagamento" label="Forma de pagamento" style="width:250px;"
								styleClass="ui-inputfield ui-widget ui-state-default ui-corner-all"
								value="#{venderVeiculosBean.idFormaPagamento}" >
							    <f:selectItems value="#{venderVeiculosBean.todasFormasPagamento}" var="formaPag"
							    	itemValue="#{formaPag.value}" itemLabel="#{formaPag.label}"/>
							</h:selectOneMenu>
			            </td>
			            <td>
			            	<br />
							<p:commandButton value="Adicionar" update="msgSistema, venderVeiculosForm" image="ui-icon ui-icon-plusthick"
					 			actionListener="#{venderVeiculosBean.adicionarPagamentos}" />
			            </td>
					</tr>
					
				</table>

					<p:dataTable id="listaItens" var="lancamento" 
						widgetVar="lancamentosTableItens" style="padding-left:10px; padding-bottom:10px;"
						paginator="true" rows="10" paginatorPosition="bottom"
						value="#{venderVeiculosBean.lancamentos}"
			            emptyMessage="Nenhum pagamento cadastrado">
			            
				        <p:column style="width:6%;" 
				                headerText="Valor" filterMatchMode="contains">
				            <h:outputText value="#{lancamento.valorTotal}" converter="moedaDoubleConverter" />
				        </p:column>
				        <p:column style="width:6%;" 
				                headerText="Data" filterMatchMode="contains">
				            <h:outputText value="#{lancamento.dtVencimento}" />
				        </p:column>
				        <p:column style="width:6%;" 
				                headerText="Categoria" filterMatchMode="contains">
				            <h:outputText value="#{lancamento.categoria.nmCategoria}" />
				        </p:column>
				        <p:column style="width:10%;" 
				                headerText="Conta" filterMatchMode="contains">
				            <h:outputText value="#{lancamento.conta.contaCompleto}" />
				        </p:column>
				        <p:column style="width:6%;" 
				                headerText="Forma de pagamento" filterMatchMode="contains">
				            <h:outputText value="#{lancamento.formaPagamento.descricao}" />
				        </p:column>
				        
				        <p:column style="width:3%;" headerText="Ações">
				            <p:commandButton oncomplete="confirmationPagamento.show()" 
				            	image="ui-icon ui-icon-trash" immediate="true">
				               <f:setPropertyActionListener value="#{lancamento}"
				                    target="#{venderVeiculosBean.lancamentoExclusaoAux}" />
				            </p:commandButton>
				        </p:column>
				    </p:dataTable>
				    <p align="right">
						 <h:outputLabel style="font-size:14px;" value="Total: " />
						 <h:outputText value="#{venderVeiculosBean.totalItens}" converter="moedaDoubleConverter" />
					</p>
				    
				<br />				
				
			</p:panel>
			
			<br />
			<div align="right">
				<p:commandButton id="btn" value="Salvar venda" update="msgSistema, venderVeiculosForm, listaVeiculosVendidos" 
					actionListener="#{venderVeiculosBean.cadastrarVendaVeiculo}" style="margin-left:5px;" />
				<p:commandButton id="botaoLimpar" value="Limpar" update="msgSistema, venderVeiculosForm" 
					 actionListener="#{venderVeiculosBean.limparCampos}" style="margin-left:5px;" />
			</div>
				
		</h:form>
		
		<h:form id="listaVeiculosVendidos">
			
			<br />
			<br />
			<p:accordionPanel autoHeight="false" collapsible="true" style="padding:0;">
			<p:tab title="Veículos vendidos">
			
				<p:dataTable id="listaVendasVeiculo" var="venda" 
					widgetVar="vendasVeicTable"
					paginator="true" rows="10" paginatorPosition="bottom"
					value="#{venderVeiculosBean.todasVendas}"
		            emptyMessage="Nenhuma venda de veículo encontrada">
		            
		            <p:column style="width:140px;" filterBy="#{venda.veiculo.nrPlaca}"
			                headerText="Placa" filterMatchMode="contains">
			            <h:outputText value="#{venda.veiculo.nrPlaca}" />
			        </p:column>
			        
			        <p:column style="width:140px;" filterBy="#{venda.cliente.nmPessoa}"
			                headerText="Cliente" filterMatchMode="contains">
			            <h:outputText value="#{venda.cliente.nmPessoa}" />
			        </p:column>
			        
			        <p:column style="width:140px;" filterBy="#{venda.dtVenda}"
			                headerText="Data da venda" filterMatchMode="contains">
			            <h:outputText value="#{venda.dtVenda}" />
			        </p:column>
			        
			        <p:column style="width:140px;" filterBy="#{venda.vlrVenda}"
			                headerText="Valor de venda" filterMatchMode="contains">
			            <h:outputText value="#{venda.vlrVenda}" converter="moedaDoubleConverter" />
			        </p:column>
			        
			        <p:column style="width:140px;" filterBy="#{venda.valorTotal}"
			                headerText="Total de pagametos" filterMatchMode="contains">
			            <h:outputText value="#{venda.valorTotal}" converter="moedaDoubleConverter" />
			        </p:column>
			        
			        <p:column style="width:100px;" headerText="Ações">
			            <p:commandButton update="vendaDetalhesForm" image="ui-icon ui-icon-search" immediate="true"
			            	oncomplete="dialogVendaDetalhes.show()">
			               <f:setPropertyActionListener value="#{venda}"
			                    target="#{venderVeiculosBean.vendaVeiculoAux}" />
			            </p:commandButton>
			            <p:commandButton update="venderVeiculosForm" image="ui-icon ui-icon-pencil" immediate="true" >
			               <f:setPropertyActionListener value="#{venda}" 
			                    target="#{venderVeiculosBean.vendaVeiculo}" />
			            </p:commandButton>
			            <p:commandButton oncomplete="confirmation.show()" image="ui-icon ui-icon-trash" immediate="true">
			               <f:setPropertyActionListener value="#{venda}"
			                    target="#{venderVeiculosBean.vendaVeiculoAux}" />
			            </p:commandButton>
			        </p:column>
			        
			    </p:dataTable>
			    
		    </p:tab>
			</p:accordionPanel>
		    
		</h:form>
		
		<h:form>
		    <p:confirmDialog message="Deseja excluir o registro?"
						header="Confirmação" severity="alert" widgetVar="confirmation">
						
				<p:commandButton value="Sim" update="venderVeiculosForm, msgSistema, listaVeiculosVendidos" 
							oncomplete="confirmation.hide()" immediate="true"
							actionListener="#{venderVeiculosBean.excluirVenda}" />
				<p:commandButton value="Não" onclick="confirmation.hide()" type="button" immediate="true" /> 
						
			</p:confirmDialog>
		</h:form>
		
		<h:form>
		    <p:confirmDialog message="Deseja excluir o pagamento?"
						header="Confirmação" severity="alert" widgetVar="confirmationPagamento">
						
				<p:commandButton value="Sim" update="venderVeiculosForm, msgSistema" 
							oncomplete="confirmationPagamento.hide()" immediate="true"
							actionListener="#{venderVeiculosBean.excluirPagamento}" />
				<p:commandButton value="Não" onclick="confirmationPagamento.hide()" type="button" immediate="true" /> 
						
			</p:confirmDialog>
		</h:form>
			
	<ui:include src="vendadetalhes.xhtml" />
	<ui:include src="../manterveiculos/veiculomodal.xhtml" />
	<ui:include src="../clientesfornecedores/clientefornecedormodal.xhtml" />
	
	</ui:define>

</ui:composition>
</html>